package com.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.biz.*;
import com.controller.backend.BaseController;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.cond.BaseCond;
import com.entity.model.*;
import com.entity.vo.HotelVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.util.CalendarUtil;
import com.util.webpage.PageData;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jc on 2019/11/16.
 */
@RestController("apiHotelController")
@RequestMapping("/api/hotel")
public class HotelController extends BaseController<Hotel> {
    @Autowired
    private HotelBiz hotelBiz;

    @Autowired
    private HotelHomeBiz hotelHomeBiz;

    @Autowired
    private HotelPhotoBiz hotelPhotoBiz;

    @Autowired
    private HotelReservationBiz hotelReservationBiz;

    //已开放城市
    @RequestMapping(value = "/openCity")
    public Result openCity() {
        List<Hotel> hotels = hotelBiz.findOpenCity();
        String str = listToString(hotels);
        return ResultGenerator.genSuccessResult(str);
    }

    //民宿详情
    @RequestMapping(value = "/detail")
    public Result detail(@RequestParam Integer hotelId, Integer hotelHomeId) {
        if (hotelId == null) {
            return ResultGenerator.genFailResult("民宿编号不能为空");
        }
        Hotel hotel = hotelBiz.findById(hotelId);
        if (hotel != null) {
            Condition cond = new Condition(HotelHome.class);
            cond.createCriteria().andEqualTo("hotelId", hotel.getId()).andEqualTo("status", 0);
            List<HotelHome> list = hotelHomeBiz.findByCondition(cond);
            for (Integer i = 0; i < list.size(); i++) {
                if (list.get(i).getHomePhoto() != null) {
                    String str = list.get(i).getHomePhoto().split(",")[0];
                    list.get(i).setHomePhoto(str);
                }
            }
            hotel.setHotelHomeList(list);

            if (hotelHomeId != null) {
                cond = new Condition(HotelHome.class);
                cond.createCriteria().andEqualTo("id", hotelHomeId);
                HotelHome hotelHome = hotelHomeBiz.findByCondition(cond).get(0);
                hotel.setHotelHome(hotelHome);
            }

            cond = new Condition(HotelPhoto.class);
            cond.createCriteria().andEqualTo("hotelId", hotelId);
            List<HotelPhoto> hotelPhotoList = hotelPhotoBiz.findByCondition(cond);
            hotel.setHotelPhotoList(hotelPhotoList);

        } else {
            return ResultGenerator.genFailResult("民宿不存在");
        }
        return ResultGenerator.genSuccessResult(hotel);
    }

    //民宿列表
    @RequestMapping(value = "/hotelList")
    public Result hotelList(String hotelName, Integer hotelType, BaseCond bcond) throws Exception {
        PageHelper.startPage(bcond.getPageNum(), bcond.getPageSize(), false);
        Map cond = new HashMap();
        cond.put("hotelName", hotelName);
        cond.put("hotelType", hotelType);
        List<HotelVO> hotels = hotelBiz.getHotelList(cond);

        for (Integer i = 0; i < hotels.size(); i++) {
            Condition condition = new Condition(HotelPhoto.class);
            condition.orderBy("id").asc();
            condition.createCriteria().andEqualTo("hotelId", hotels.get(i).getId());
            List<HotelPhoto> hotelPhotoList = hotelPhotoBiz.findByCondition(condition);
            if (hotelPhotoList.size() > 0) {
                hotels.get(i).setHotelPhoto(hotelPhotoList.get(0).getUrl());
            }
        }
        return ResultGenerator.genSuccessResult(hotels);
    }


    public String listToString(List<Hotel> list) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i).getCityName()).append(",");
        }
        return sb.toString().substring(0, sb.toString().length() - 1);
    }


    //本周上新
    @RequestMapping(value = "/newThisWeek")
    public Result newThisWeek(BaseCond bcond) throws Exception {
        PageHelper.startPage(bcond.getPageNum(), bcond.getPageSize(), false);

        List<Hotel> hotels = hotelBiz.getNewThisWeek();
        for (Integer i = 0; i < hotels.size(); i++) {
            Condition cond = new Condition(HotelPhoto.class);
            cond.createCriteria().andEqualTo("hotelId", hotels.get(i).getId());
            List<HotelPhoto> hotelPhotoList = hotelPhotoBiz.findByCondition(cond);
            hotels.get(i).setHotelPhotoList(hotelPhotoList);

            cond = new Condition(HotelHome.class);
            cond.createCriteria().andEqualTo("hotelId", hotels.get(i).getId());
            HotelHome hotelHome = hotelHomeBiz.findByCondition(cond).get(0);
            hotels.get(i).setHotelHome(hotelHome);
        }

        return ResultGenerator.genSuccessResult(hotels);
    }


    //本周上新
    @RequestMapping(value = "/priceInterval")
    public Result priceInterval(BaseCond bcond) throws Exception {
        return ResultGenerator.genSuccessResult();
    }
}
